﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Xml;
using System.Configuration;
using WMM.Tapioca.Instance;
using WMM.Tapioca;
using System.Diagnostics;
using WMM.Tapioca.Utilities;

namespace ConsoleApplicationTeste
{
    class Program
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        static string fileName;

        static bool validateDTS = false;
        static bool validateCalculation = false;
        static bool validateFormula = false;

        static string proxy;
        static Dictionary<string, string> parameters = new Dictionary<string, string>();

        static void Main(string[] args)
        {
            for (int i = 0; i < args.Length; i++)
            {
                 switch (args[i])
                {
                    case "-f":
                    case "-file":
                        if ((i + 1) < args.Length)
                        {
                            fileName = args[i + 1];
                        }
                        break;
                    case "-help":
                        ShowHelp();
                        return;
                    case "-parameters":
                        int k = 0;
                        while (!string.IsNullOrEmpty(args[i + k + 1]) && !args[i + k + 1].StartsWith("--"))
                        {
                            if (!string.IsNullOrEmpty(args[i + k + 1]) && !string.IsNullOrEmpty(args[i + k + 2]) && 
                                !args[i + k + 1].StartsWith("--") && !args[i + k + 2].StartsWith("--"))
                            {
                                parameters.Add(args[i + k + 1], args[i + k + 2]);
                            }

                            k = k + 1;
                        }
                        break;
                    case "-proxy":
                        if ((i + 1) < args.Length)
                        {
                            proxy = args[i + 1];
                        }
                        break;
                    case "-validateCalculation":
                        validateCalculation = true;
                        break;
                    case "-validateDTS":
                        validateDTS = true;
                        break;
                    case "-validateFormula":
                        validateFormula = true;
                        break;                    
                    default:
                        if (args[i].StartsWith("-"))
                        {
                            Console.WriteLine(Process.GetCurrentProcess().ProcessName + ": error: no such option: " + args[i]);
                            return;
                        }
                        break;
                }
                
            }
           
            foreach (string s in args)
            {
                Console.WriteLine(s);
            }

            XmlCachingResolver resolver = null;

            if (!string.IsNullOrEmpty(proxy))
            {
                resolver = new XmlCachingResolver(true, true);
                resolver.GetProxy(proxy);
            }
            else
            {
                resolver = new XmlCachingResolver(true, false);
            }

            if (string.IsNullOrEmpty(fileName))
            {
                Console.WriteLine("Parameter [fileName] can't be null or empty");
                return;
            }
            //InstanceDocument xbrlDoc = new InstanceDocument(@"..\..\..\WMM.Test.Tapioca\top50\Instancia-TOP50-201206.xml", resolver);
            InstanceDocument xbrlDoc = new InstanceDocument(fileName, resolver);
            
            //log.Info("Info logging");
            XbrlManager xbrlManager = new XbrlManager(xbrlDoc);
            xbrlManager.IsValidateCalculation = validateCalculation;
            xbrlManager.IsValidateDTS = validateDTS;
            xbrlManager.IsValidateFormula = validateFormula;
            xbrlManager.Validate();
            bool result = xbrlDoc.Dts.ValidatedInstance;

        }

        private static void ShowHelp()
        {
            Console.WriteLine("\nOptions:\n");
            Console.WriteLine("  -f ENTRYPOINTFILE, --file=ENTRYPOINTFILE");
            Console.WriteLine("\t\t\t FILENAME is an entry point, which may be an XBRL");
            Console.WriteLine("\t\t\t instance, schema, linkbase file, inline XBRL instance.\n");
            Console.WriteLine("  -parameters=PARAMETERS  Specify parameters for formula and validation");
            Console.WriteLine("\t\t\t (name=value[,name=value]).\n");
            Console.WriteLine("  -validateCalculation\t Specify calculation linkbase validation \n");
            Console.WriteLine("  -validateDimension\t Specify dimension validation\n");
            Console.WriteLine("  -validateFormula\t Specify formula validation");
        }

    }
}
